﻿using HardcoreGame.Common.Abstract;
using HardcoreGame.Common.Cores.Seasons;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class EventSystem : ModSystem
    {
        private static bool dayTimeLast;
        public static bool dayTimeSwitched;
        public static List<int> activeEvents = [];
        public override void OnWorldLoad()
        {
            activeEvents = [];
        }
        public override void SaveWorldData(TagCompound tag)
        {
            tag["activeEvents"] = activeEvents;
        }
        public override void LoadWorldData(TagCompound tag)
        {
            if (tag.ContainsKey("activeEvents"))
            {
                activeEvents = (List<int>)tag.GetList<int>("activeEvents");
            }
            else
            {
                activeEvents = [];
            }
        }
        public override void PostUpdateWorld()
        {
            if (Main.dayTime != dayTimeLast)
            {
                dayTimeSwitched = true;
            }
            else
            {
                dayTimeSwitched = false;
            }
            dayTimeLast = Main.dayTime;
            if (dayTimeSwitched)
            {
                activeEvents = [];
            }
            if (dayTimeSwitched && activeEvents.Count == 0)
            {
                if (Main.rand.NextBool(18) && Main.hardMode)
                {
                    StartEvent(1);
                }
                if (Main.rand.NextBool(12) && !Main.hardMode && NPC.downedBoss1)
                {
                    StartEvent(1);
                }
                SeasonSystem seasonSystem = ModContent.GetInstance<SeasonSystem>();
                Season currentSeason = seasonSystem.currentSeason;
                if (currentSeason is Summer && Main.raining && !activeEvents.Contains(1) && Main.rand.NextBool(18))
                {
                    Main.NewText(Language.GetTextValue("Mods.HardcoreGame.Tips.12"), Colors.RarityOrange);
                    StartEvent(5);
                }
            }
            if (!Main.dayTime && dayTimeSwitched && activeEvents.Count == 0 && Main.rand.NextBool(16))
            {
                StartEvent(4);
            }
            if (Main.dayTime && activeEvents.Contains(4))
            {
                EndEvent(4);
            }
            if (dayTimeSwitched && activeEvents.Count == 0 && Main.rand.NextBool(16))
            {
                StartEvent(2);
                Main.NewText(Language.GetTextValue("Mods.HardcoreGame.Tips.1"), Colors.RarityOrange);
            }
            if (dayTimeSwitched && activeEvents.Contains(2) && Main.rand.NextBool(6))
            {
                EndEvent(2);
                Main.NewText(Language.GetTextValue("Mods.HardcoreGame.Tips.2"), Colors.RarityGreen);
            }
            if (dayTimeSwitched && activeEvents.Count == 0 && Main.rand.NextBool(19))
            {
                StartEvent(3);
                Main.NewText(Language.GetTextValue("Mods.HardcoreGame.Tips.4"), Colors.RarityOrange);
            }
            if (dayTimeSwitched && activeEvents.Contains(2) && Main.rand.NextBool(6))
            {
                EndEvent(3);
                Main.NewText(Language.GetTextValue("Mods.HardcoreGame.Tips.5"), Colors.RarityGreen);
            }
            if (!Main.raining)
            {
                EndEvent(1);
                EndEvent(5);
            }            
        }
        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            if (activeEvents.Contains(3))
            {
                layers.RemoveAll(layer => layer.Name.Contains("Minimap"));
            }
        }
        public void StartEvent(int id)
        {
            if (Main.netMode != NetmodeID.SinglePlayer)
            {
                ModPacket packet = Mod.GetPacket(256);
                packet.Write(0);
                packet.Write(id);
                packet.Send(-1, -1);
            }
            activeEvents.Add(id);
        }
        public void EndEvent(int id)
        {
            if (Main.netMode != NetmodeID.SinglePlayer)
            {
                ModPacket packet = Mod.GetPacket(256);
                packet.Write(1);
                packet.Write(id);
                packet.Send(-1, -1);
            }
            activeEvents.Remove(id);
        }

    }
}